home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / raw2ilbm.c < prev    next >
C/C++ Source or Header  |  1986-04-20  |  6KB  |  197 lines

  1.  
  2. /** raw2ilbm.c **************************************************************
  3. /*  Read in a "raw" bitmap (dump of the bitplanes in a screen)  */
  4. /*  Display it, and write it out as an ILBM file.      */
  5. /*  23-Jan-86                                */
  6. /*                                      */
  7. /*  Usage from CLI: 'Raw2ILBM  source dest fmt(low,med,hi)  */
  8. /*     nplanes'                                   */
  9. /*  Supports the three common Amiga screen formats.         */
  10. /*        'low' is 320x200,                  */
  11. /*        'med' is 640x200,                  */
  12. /*        'hi' is 640x400.                   */
  13. /*         'nplanes' is the number of bitplanes.       */
  14. /*  The default is low-resolution, 5 bitplanes              */
  15. /*        (32 colors per pixel).                  */
  16. /*                                                              */
  17. /* By Jerry Morrison and Steve Shaw, Electronic Arts.           */
  18. /* This software is in the public domain.                       */
  19. /*                                                              */
  20. /* This version for the Commodore-Amiga computer.               */
  21. /*                                                              */
  22. /****************************************************************/
  23.  
  24. #include "iff/intuall.h"
  25. #include "libraries/dos.h"
  26. #include "libraries/dosextens.h"
  27. #include "iff/ilbm.h"
  28. #include "iff/putpict.h"
  29.  
  30. #define MIN(a,b) ((a)<(b)?(a):(b))
  31. #define MAX(a,b) ((a)>(b)?(a):(b))
  32.  
  33. /* general usage pointers */
  34. LONG IconBase; /* Actually, "struct IconBase *" if you've got some ".h" file*/
  35. struct GfxBase *GfxBase;
  36.  
  37.  
  38. /* Globals for displaying an image */
  39. struct RastPort rP;
  40. struct RasInfo rasinfo;
  41. struct View v = {0};
  42. struct ViewPort vp = {0};
  43. struct View *oldView = 0;     /* so we can restore it */
  44.  
  45. /* ---------------------------------- */
  46. DisplayPic(bm, colorMap) struct BitMap *bm; UWORD *colorMap;  {
  47.  
  48.     oldView = GfxBase->ActiView;   /* so we can restore it */
  49.          
  50.     InitView(&v);
  51.     InitVPort(&vp);
  52.     v.ViewPort = &vp;
  53.     InitRastPort(&rP);
  54.     rP.BitMap = bm;
  55.     rasinfo.BitMap = bm;
  56.  
  57.     /* Always show the upper left-hand corner of this picture. */
  58.     rasinfo.RxOffset = 0;
  59.     rasinfo.RyOffset = 0;
  60.  
  61.     vp.DWidth = bm->BytesPerRow*8; /* Physical display WIDTH */
  62.     vp.DHeight = bm->Rows;    /* Display height */
  63.  
  64.     /* Always display it in upper left corner of screen.*/
  65.  
  66.     if (vp.DWidth <= 320) vp.Modes = 0;
  67.      else vp.Modes = HIRES;
  68.     if (vp.DHeight > 200) {
  69.      v.Modes |= LACE;
  70.      vp.Modes |= LACE;
  71.      }
  72.     vp.RasInfo = &rasinfo;
  73.     MakeVPort(&v,&vp);
  74.     MrgCop(&v);
  75.     LoadView(&v);   /* show the picture */
  76.     WaitBlit();
  77.     WaitTOF();
  78.     if (colorMap) LoadRGB4(&vp, colorMap,(1 << bm->Depth));
  79.     }
  80.  
  81. UnDispPict() {
  82.     if (oldView) {
  83.      LoadView(oldView);  /* switch back to old view */
  84.      FreeVPortCopLists(&vp);
  85.      FreeCprList(v.LOFCprList);
  86.      }
  87.     }
  88.  
  89. PrintS(msg)  char *msg; {   printf(msg);    }
  90.  
  91. void GoodBye(msg)  char *msg; {   PrintS(msg);   PrintS("\n");   exit(0);   }
  92.  
  93. struct BitMap bitmap = {0};
  94. SHORT cmap[32];
  95.  
  96. AllocBitMap(bm) struct BitMap *bm; {
  97.     int i;
  98.     LONG psz = bm->BytesPerRow*bm->Rows;
  99.     UBYTE *p = (UBYTE *)AllocMem(bm->Depth*psz, MEMF_CHIP|MEMF_PUBLIC);
  100.     for (i=0; i<bm->Depth; i++)  { 
  101.      bm->Planes[i] = p;
  102.      p += psz;
  103.      }
  104.     }
  105.  
  106. FreeBitMap(bm) struct BitMap *bm;  {
  107.     if (bitmap.Planes[0])  {
  108.      FreeMem(bitmap.Planes[0],
  109.           bitmap.BytesPerRow * bitmap.Rows * bitmap.Depth);
  110.      }
  111.     }
  112.  
  113. BOOL LoadBitMap(file,bm,cols)
  114.     LONG file; 
  115.     struct BitMap *bm;
  116.     SHORT *cols;
  117.     {
  118.     SHORT i;
  119.     LONG nb,plsize;
  120.     plsize = bm->BytesPerRow*bm->Rows;
  121.     for (i=0; i<bm->Depth; i++) {
  122.      nb =  Read(file, bm->Planes[i], plsize);
  123.      if (nb<plsize) BltClear(bm->Planes[i],plsize,1);
  124.      }
  125.     if (cols) {
  126.      nb = Read(file, cols, (1<<bm->Depth)*2);     /* load color map */
  127.      return( (BOOL) (nb == (1<<bm->Depth)*2) );
  128.      }
  129.     return((BOOL) FALSE);
  130.     }
  131.  
  132.  
  133. /** main() ******************************************************************/
  134.  
  135. UBYTE defSwitch[] = "b";
  136.      
  137. #define BUFSIZE  16000
  138.  
  139. static SHORT maxDepth[3] = {5,4,4};
  140.  
  141. void main(argc, argv)  int argc;  char **argv;  {
  142.     SHORT fmt,depth,pwidth,pheight;
  143.     UBYTE *buffer;
  144.     BOOL hadCmap;
  145.     LONG file;
  146.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) )
  147.      GoodBye("No graphics.library");
  148.     if( !(IconBase = OpenLibrary("icon.library",0)) )
  149.      GoodBye("No icon.library");
  150.     if (argc) {
  151.      if (argc < 3) {
  152.          printf(
  153. "Usage from CLI: 'Raw2ILBM  source dest fmt(low,med,hi) nplanes'\n");
  154.          goto bailout;
  155.          }
  156.      fmt = 0;
  157.      depth = 5;
  158.      if (argc>3)
  159.          switch(*argv[3]) {
  160.           case 'l': fmt = 0; break;
  161.           case 'm': fmt = 1; break;
  162.           case 'h': fmt = 2; break;
  163.           }
  164.      if (argc>4) depth = *argv[4]-'0';
  165.      depth = MAX(1, MIN(maxDepth[fmt],depth));
  166.      pwidth = fmt? 640: 320;
  167.      pheight = (fmt>1)? 400: 200;
  168.      InitBitMap(&bitmap, depth, pwidth, pheight);
  169.      AllocBitMap(&bitmap);
  170.      
  171.      file = Open(argv[1], MODE_OLDFILE);
  172.      
  173.      if (file)  { 
  174.          DisplayPic(&bitmap,NULL);
  175.          hadCmap = LoadBitMap(file,&bitmap, cmap);    
  176.          if (hadCmap) LoadRGB4(&vp, cmap, 1<<bitmap.Depth);
  177.          Close(file);
  178.          file = Open(argv[2], MODE_NEWFILE);
  179.          buffer = (UBYTE *)AllocMem(BUFSIZE, MEMF_CHIP|MEMF_PUBLIC);
  180.          PutPict(file, &bitmap, pwidth, pheight,
  181.           hadCmap? cmap: NULL, buffer, BUFSIZE);
  182.          Close(file);
  183.          FreeMem(buffer,BUFSIZE);
  184.          }
  185.      else printf(" Couldn't open file '%s' \n",argv[2]);
  186.      }
  187.  
  188.     UnDispPict();
  189.     FreeBitMap(&bitmap);
  190.      
  191.     bailout:
  192.      CloseLibrary(GfxBase);
  193.      CloseLibrary(IconBase);
  194.      exit(0);
  195.     }
  196.  
  197.